import { postEx, rawDelete, rawGet } from '@/services/crud'
export function setBoxInfo(boxID, ip, ipc_id, rtsp, playAddr, idinbox, b, p) {
    if (b === "" && b == undefined) {
        return new Promise((re, rej) => {
            rej("b is empty")
        })
    }
    if (p === "" && p == undefined) {
        return new Promise((re, rej) => {
            rej("p is empty")
        })
    }
    if (boxID === "" && boxID == undefined) {
        return new Promise((re, rej) => {
            rej("boxID is empty")
        })
    }
    if (idinbox === "" && idinbox == undefined) {
        return new Promise((re, rej) => {
            rej("idinbox is empty")
        })
    }
    let url = `extend/api/m_smartBoxForPolice?qmap[ctype]=boxipc`
    url += ("&qmap[ipc_id]=" + ipc_id + "&qmap[box_id]=" + boxID)
    if (rtsp) {
        url += ("&qmap[rtsp]=" + encodeURIComponent(rtsp))
    }
    if (playAddr) {
        url += ("&qmap[play_addr]=" + encodeURIComponent(playAddr))
    }
    if (idinbox) {
        url += ("&qmap[id_in_box]=" + idinbox)
    }
    let sip = ip
    if (!sip) {
        sip = "0.0.0.0"
    }
    url += ("&qmap[ip]=" + sip)
    url += ("&qmap[b]=" + b)
    url += ("&qmap[p]=" + p)

    return postEx(url, "")


}
export function createIPCs(datas){
    let url = `extend/api/m_smartBoxForPolice?qmap[ctype]=boxipc_batch`
    return postEx(url,datas)
}
export function deleteCamera(ipc_id) {
    if (ipc_id && ipc_id.length > 0) {
        return rawDelete('api/extend/api/m_smartBoxForPolice?qmap[ctype]=boxipc&qmap[ipc_id]=' + ipc_id)
    }
    return new Promise((re) => {
        re()
    })

}
export function getIPCs(box) {
    let initIPCS = []
    for (let i = 0; i < 8; i++) {
        initIPCS.push({
            id_in_box: "Cam" + i,
            sno: i,
            B: "",
            P: "",
            IP: "",
            port: 80,
            username: "admin",
            password: "",
            rtsp: "",
            play_addr: "",
            business_id: "",
            s_name: "摄像机" + i,
            ipc_id: "",
        });
    }
    return new Promise((re, rej) => {
        let ipcs = Object.assign([], initIPCS);
        if (box) {
            rawGet(
                "api/extend/api/m_smartBoxForPolice?qmap[ctype]=thing_ex&qmap[box_id]=" +
                box.uid
            ).then((res) => {
                if (
                    res &&
                    res.data &&
                    res.data.result &&
                    Array.isArray(res.data.result)
                ) {
                    let cameras = res.data.result;
                    ipcs.forEach((ipc) => {
                        ipc.isEdit = false;
                        let camera = cameras.find((c) => {
                            return c.id_in_box === ipc.id_in_box;
                        });
                        if (camera) {
                            Object.assign(ipc, camera);
                            ipc.isEdit = true;
                        }
                    });


                }
                re(ipcs)

            }).catch((err) => {
                rej(err)
            });
        } else {
            re(ipcs)
        }
    })

}
import Axios from "axios";
export function playVideo(self, videoAddr) {
    let localAddr = "";
    if (process.env.NODE_ENV === "development") {
        localAddr = "http://127.0.0.1:18869/playVideo"
    } else {
        localAddr = "http://127.0.0.1:18869/playVideo";
    }
    let axios = Axios.create({baseURL:localAddr})
    axios.get(localAddr)
        .then((res) => {
            console.log("video plugin ok", res);
            axios.put(localAddr, { Addr: videoAddr });
        })
        .catch(() => {
            self.$confirm({
                title: "下载视频播放插件",
                content: "请下载视频播放插件，该插件将为您建立视频服务",
                icon: "exclamation-circle",
                onOk: () => {
                    console.log("点击确认");
                    window.open("api/file/videopluginIOTBASE.exe")
                },
                onCancel() { },
            });
        });
}

export default {
    setBoxInfo,
    deleteCamera,
    getIPCs,
    createIPCs,
}